package com.zhaojonhy.zhaopangwb.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.CommentsAPI;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.bean.Statuses;
import com.zhaojonhy.zhaopangwb.utils.AccessTokenKeeper;
import com.zhaojonhy.zhaopangwb.utils.Constants;
import com.zhaojonhy.zhaopangwb.utils.TitleBuilder;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;

/**
 * Created by Administrator on 2016/9/26.
 */

public class WriteCommentAcyivity extends Activity implements View.OnClickListener{

    private static final String TAG = WriteCommentAcyivity.class.getSimpleName() ;

    private CommentsAPI commentApi ;
    /** 当前 Token 信息 */
    private Oauth2AccessToken mAccessToken;

    private Statuses status ;
    private EditText et_write_status ;
    private ImageView iv_image ;
    private ImageView iv_at ;
    private ImageView iv_topic ;
    private ImageView iv_emoji ;
    private ImageView iv_add ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_status);

        status = (Statuses) getIntent().getSerializableExtra("status") ;
        initView() ;

    }

    private void initView() {

        new TitleBuilder(this)
                .setTitleText("发微博")
                .setLeftText("取消")
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //取消发送评论，关闭本页面
                        WriteCommentAcyivity.this.finish();
                    }
                })
                .setRightText("发送")
                .setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendComment() ;
            }
        });

        et_write_status = (EditText)findViewById(R.id.et_write_status) ;
        iv_image = (ImageView) findViewById(R.id.iv_image) ;
        iv_at = (ImageView)findViewById(R.id.iv_at) ;
        iv_topic = (ImageView)findViewById(R.id.iv_topic) ;
        iv_emoji = (ImageView)findViewById(R.id.iv_emoji) ;
        iv_add = (ImageView)findViewById(R.id.iv_add) ;

        iv_image.setOnClickListener(this);
        iv_at.setOnClickListener(this);
        iv_topic.setOnClickListener(this);
        iv_emoji.setOnClickListener(this);
        iv_add.setOnClickListener(this);
    }

    private void sendComment() {
        String comment = et_write_status.getText().toString();
        if(TextUtils.isEmpty(comment)) {
            ToastUtils.showToast(this,"评论内容不能为空", Toast.LENGTH_LONG);
            return;
        }
        Log.d(TAG,"success--? "+"yes") ;
        // 获取当前已保存过的 Token
        mAccessToken = AccessTokenKeeper.readAccessToken(this);
        commentApi = new CommentsAPI(this, Constants.APP_KEY, mAccessToken) ;
        commentApi.create(comment, status.getId(), true, new RequestListener() {
            @Override
            public void onComplete(String response) {

                Log.d(TAG,"success--? "+"yes"+response) ;
                ToastUtils.showToast(WriteCommentAcyivity.this,"发送成功", Toast.LENGTH_LONG);

                //微博发送成功后，设置Result结果数据，然后关闭本页面
                Intent data = new Intent() ;
                data.putExtra("sendCommentSuccess",true) ;
                setResult(RESULT_OK,data);

                WriteCommentAcyivity.this.finish();
            }

            @Override
            public void onWeiboException(WeiboException e) {
                Log.d(TAG,"success--? "+"no") ;

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_write_status :
                break;
            case R.id.iv_image :
                break;
            case R.id.iv_at :
                break;
            case R.id.iv_topic :
                break;
            case R.id.iv_emoji :
                break;
            case R.id.iv_add :
                break;
            default:
                break;
        }
    }
}
